﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MsForgeReboot.Framework.Entities;
//using WebMatrix.WebData;
using System.Web.Security;
using MsForgeReboot.Framework.ViewModels;
using MsForgeReboot.Framework.Interfaces;

namespace MsForgeReboot.Web.Areas.ControlPanel.Controllers {
    //[Authorize]
    public class HomeController : Controller {

        //private SimpleRoleProvider _roleProvider;

        //public HomeController() : this(new SimpleRoleProvider()) { }

        //public HomeController(SimpleRoleProvider p) {
        //    this._roleProvider = p;
        //    //WebSecurity.InitializeDatabaseConnection("MsForgeContext", "Users", "Id", "UserName", autoCreateTables: true);

        //}

        private readonly IUserService _svc;

        public HomeController(IUserService svc) {
            this._svc = svc;
        }

        public ActionResult Index() {
            ViewBag.Title = "Control Panel";
            return View();
        }

        [HttpGet]
        [ActionName("Roles")]
        public ActionResult Manage(ActionMessageId? message) {
            //string[] roles = this._roleProvider.GetAllRoles();
            string[] roles = Roles.GetAllRoles();
            ViewBag.Roles = roles;
            ViewBag.StatusMessage = message == ActionMessageId.RoleCreated ? "Rola kreirana uspešno"
                : message == ActionMessageId.RoleDeleted ? "Rola obrisana uspešno"
                : message == ActionMessageId.UsersAddedToRole ? "Korisnici su dodati u rolu uspešno" 
                : message == ActionMessageId.UserAlreadyInRole ? "Korisnici već postoje u Roli!"
                : message == ActionMessageId.IvalidRoleName ? "Ime role nije validno (ili je prazno)" 
                : "";
            return View("Roles");
        }

        [HttpPost]
        [ActionName("Roles")]
        public ActionResult Manage(string item) {
            if (!String.IsNullOrEmpty(item)) {
                Roles.CreateRole(item);
                return RedirectToAction("Roles", new { message = ActionMessageId.RoleCreated });
            }
            return RedirectToAction("Roles");
        }

        [HttpGet]
        public ActionResult Delete(string item) {
            Roles.DeleteRole(item);
            return RedirectToAction("Roles", new { message = ActionMessageId.RoleDeleted });
        }

        [HttpPost]
        public ActionResult UsersToRoles(UsersRolesViewModel model) {
            if (ModelState.IsValid) {
                try {
                    var users = this._svc.GetUsernames(model.Users);
                    Roles.AddUsersToRole(users, model.Role);
                    return RedirectToAction("Roles", new { message = ActionMessageId.UsersAddedToRole });
                } catch (InvalidOperationException e) {
                    return RedirectToAction("Roles", new { message = ActionMessageId.UserAlreadyInRole });
                }
            }
            return View();
        }


        public ActionResult FilterUsers() {
            string q = Request.QueryString["q"];
            //var usernames = this._svc.FilterUsersByUsername(q).Select(u => u.Username).ToArray();
            var usernames = this._svc.SearchTopTenUsers(q).Select(u => new { id = u.Id, text = u.Username }).ToArray();
            return Json(usernames, JsonRequestBehavior.AllowGet);

        }

        [HttpGet]
        public ActionResult Users() {
            var users = this._svc.GetUsers();
            return PartialView("UsersList", users);
        }

        [HttpPost]
        public ActionResult Users(FormCollection collection) {
            return RedirectToAction("User");
        }

        

    }
    public enum ActionMessageId {
        RoleCreated,
        RoleDeleted,
        UsersAddedToRole,
        UserAlreadyInRole,
        IvalidRoleName
    }

    //public enum ErrorMessageId {
    //    InvalidRoleName,
    //    DuplicateRoleName,
    //    GenericError
    //}
} ;